#!/bin/bash
# STM32 Firmware Side-Channel AttackAnalyzeExampleScript

echo "=============================================="
echo "  STM32 Firmware Side-Channel AttackAnalyzeExample"
echo "=============================================="
echo ""

# CheckYesNoIn正确的Directory
if [ ! -f "analyze_firmware_traces.py" ]; then
    echo "Error: 请InbinsimDirectory下Run此Script"
    exit 1
fi

# Example1: Analyzestm32_aes的预ProcessTrace
echo "Example1: AnalyzeSTM32 AES预ProcessTrace"
echo "----------------------------------------------"
if [ -f "stm32_aes/traces_preprocessed.npz" ]; then
    echo "ExecuteCPAAttack..."
    python3 analyze_firmware_traces.py stm32_aes/traces_preprocessed.npz --attacks cpa
    echo ""
else
    echo "未找To stm32_aes/traces_preprocessed.npz"
    echo "请先Run: cd stm32_aes && python3 collect_traces.py --traces 100"
    echo ""
fi

# Example2: IfHas原始高频Trace，也Analyze一下
echo "Example2: AnalyzeSTM32 AES高频Trace"
echo "----------------------------------------------"
if [ -f "stm32_aes/traces_highfreq.npz" ]; then
    echo "ExecuteCPAAttack..."
    python3 analyze_firmware_traces.py stm32_aes/traces_highfreq.npz --attacks cpa
    echo ""
else
    echo "未找To stm32_aes/traces_highfreq.npz"
    echo ""
fi

# Example3: 采集新的Trace并Analyze
echo "Example3: 采集新Trace并Analyze"
echo "----------------------------------------------"
read -p "YesNo采集新的100条AESTrace? (y/n): " answer
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
    echo "进入stm32_aesDirectory..."
    cd stm32_aes
    
    # 采集Trace
    echo "采集Trace中..."
    python3 collect_traces.py --traces 100 --output ../new_traces.npz
    
    cd ..
    
    # Analyze
    if [ -f "new_traces.npz" ]; then
        echo "Analyze新采集的Trace..."
        python3 analyze_firmware_traces.py new_traces.npz --attacks cpa dpa
    fi
else
    echo "Skip采集"
fi

echo ""
echo "=============================================="
echo "  ExampleCompleted"
echo "=============================================="
